home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / OpenInventorLab / labSolutions / walk3.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.1 KB  |  146 lines

  1. //-----------------------------------------------------------------------
  2. //
  3. //  SOLUTION: Inventor Barcelona Lab #3
  4. //
  5. //    This program illustrates a simple Inventor program which reads
  6. //    in an Inventor file and views it with the `Walkthrough viewer'.
  7. //    If the `Table' is picked then play some audio...
  8. //    If the `lamp' is picked then play some audio...
  9. //
  10. //    EXERCISE:
  11. //    Turn the `LampSwitch' on/off each time the `Lamp' is picked.
  12. //
  13. //    Hint: Find the named object `LampSwitch' in the file...
  14. //
  15. //    Type: make walk3
  16. //
  17. //-----------------------------------------------------------------------
  18.  
  19. #include <stdio.h>
  20. #include <Inventor/SoDB.h>
  21. #include <Inventor/actions/SoSearchAction.h>
  22. #include <Inventor/events/SoMouseButtonEvent.h>
  23. #include <Inventor/nodes/SoEventCallback.h>
  24. #include <Inventor/nodes/SoSeparator.h>
  25. #include <Inventor/nodes/SoSwitch.h>
  26. #include <Inventor/Xt/SoXt.h>
  27. #include <Inventor/Xt/viewers/SoXtWalkViewer.h>
  28.  
  29.  
  30. SoSwitch *lampSwitch;
  31.  
  32. void lampCallback( void *, SoEventCallback *cb ) {
  33.     // Make sure that this is a MOUSE PRESS event
  34.     if ( !SO_MOUSE_PRESS_EVENT( cb->getEvent(), ANY )) return;
  35.  
  36.     if ( lampSwitch->whichChild.getValue() == SO_SWITCH_NONE ) {
  37.     // Lamp is OFF - turn it ON...
  38.     lampSwitch->whichChild = 0;
  39.     system( "playaiff sounds/lampON.aiff &" );
  40.     }
  41.     else {
  42.     // Lamp is ON - turn it OFF...
  43.     lampSwitch->whichChild = SO_SWITCH_NONE;
  44.     system( "playaiff sounds/lampOFF.aiff &" );
  45.     }
  46. }
  47.  
  48. void lampSwitchInit( SoSeparator *root )
  49.     // Find the `Lamp' and the `Lamp Switch' nods.
  50.     SoGroup *lamp = (SoGroup *) root->getByName( "Lamp" );
  51.     lampSwitch = (SoSwitch *) root->getByName( "LampSwitch" );
  52.  
  53.     if ( (lamp == NULL) || (lampSwitch == NULL)
  54.         || (!lamp->isOfType( SoGroup::getClassTypeId()))
  55.         || (!lampSwitch->isOfType( SoSwitch::getClassTypeId())) ) {
  56.     printf( "DEBUG: `Lamp' or `LampSwitch' not found, or wrong types\n" );
  57.     exit( 0 );
  58.     }
  59.  
  60.     // Get the path to `Lamp'
  61.     SoSearchAction sa;
  62.     sa.setFind( SoSearchAction::NODE );
  63.     sa.setNode( lamp );
  64.     sa.apply( root );
  65.     SoPath *path = sa.getPath();
  66.  
  67.     // Create an event callback node that calls a function if the
  68.     // `Lamp' is picked.
  69.  
  70.     SoEventCallback *lampCB = new SoEventCallback;
  71.     lampCB->setPath(path);
  72.     lampCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(),
  73.                 lampCallback );
  74.     lamp->addChild( lampCB );
  75. }
  76.  
  77.  
  78. void tableCallback( void *, SoEventCallback *cb ) {
  79.     // Make sure that this is a MOUSE PRESS event
  80.     if ( !SO_MOUSE_PRESS_EVENT( cb->getEvent(), ANY )) return;
  81.  
  82.     system( "playaiff sounds/table.aiff &" );
  83. }
  84.  
  85.  
  86. void tableInit( SoSeparator *root )
  87.     // Find the `Table' node
  88.     SoGroup *table = (SoGroup *) root->getByName( "Table" );
  89.  
  90.     if ( (table == NULL) || (!table->isOfType( SoGroup::getClassTypeId())) ) {
  91.     printf( "DEBUG: `Table' not found or wrong type\n" );
  92.     exit( 0 );
  93.     }
  94.  
  95.     // Get the path to `Table'
  96.     SoSearchAction sa;
  97.     sa.setFind( SoSearchAction::NODE );
  98.     sa.setNode( table );
  99.     sa.apply( root );
  100.     SoPath *path = sa.getPath();
  101.  
  102.     // Create an event callback node that calls a function if the
  103.     // `Table' is picked.
  104.  
  105.     SoEventCallback *tableCB = new SoEventCallback;
  106.     tableCB->setPath(path);
  107.     tableCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(),
  108.                 tableCallback );
  109.     table->addChild( tableCB );
  110. }
  111.  
  112.  
  113. main(int argc, char **argv)
  114. {
  115.     // Initialize Inventor
  116.     Widget myWindow = SoXt::init( argv[0] );
  117.     if (myWindow == NULL) exit(1);
  118.  
  119.     // Make a viewer part of the window
  120.     SoXtWalkViewer *viewer = new SoXtWalkViewer(myWindow);
  121.  
  122.     // Read the object from a file
  123.     SoInput myInput;
  124.     if (!myInput.openFile("./scene.iv")) return(1);
  125.     SoSeparator *scene = SoDB::readAll(&myInput);
  126.     if (scene == NULL) {
  127.     printf( "Error: scene.iv file read failed!\n" );
  128.     exit(1);
  129.     }
  130.     scene->ref();
  131.  
  132.     // Search for the different objects by name
  133.     lampSwitchInit( scene );
  134.     tableInit( scene );
  135.  
  136.     // Viewer setup
  137.     viewer->setSceneGraph( scene );
  138.     viewer->setTitle( "Walkthrough Program" );
  139.     viewer->show();
  140.     SoXt::show(myWindow);
  141.  
  142.     SoXt::mainLoop();
  143. }
  144.